using System.Text;
using System;
using System.Data;
using System.Data.SqlClient;
using Csla;
using Csla.Data;
using Csla.Validation;
using BA.DAL;
using BA;

namespace BlazeApps.Library.Template
{
    [Serializable()]
    public class TemplateChildDetail : Csla.BusinessBase<TemplateChildDetail>
    {
        private BlazeApps.Library.ConfigSettings Config = new BlazeApps.Library.ConfigSettings();
        private BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
        private Procedures Procs = new Procedures();

        #region Business Properties and Methods

        //declare members
        private Guid _id = Guid.NewGuid();
        private Guid _templateid = Guid.NewGuid();
        private int _placeholdernumber = 1;
        private string _fieldname = string.Empty;
        private int _fieldtype = 0;
        private int _fieldheight = 0;
        private int _fieldwidth = 0;
        private int _editortype = 0;
        private Boolean _ShowName = true;

        [System.ComponentModel.DataObjectField(true, true)]
        public Guid id
        {
            get
            {
                CanReadProperty("id", true);
                return _id;
            }
        }

        public Guid templateid
        {
            get
            {
                CanReadProperty("templateid", true);
                return _templateid;
            }
            set
            {
                CanWriteProperty("templateid", true);
                if (!_templateid.Equals(value))
                {
                    _templateid = value;
                    PropertyHasChanged("templateid");
                }
            }
        }

        public int placeholdernumber
        {
            get
            {
                CanReadProperty("placeholdernumber", true);
                return _placeholdernumber;
            }
            set
            {
                CanWriteProperty("placeholdernumber", true);
                if (!_placeholdernumber.Equals(value))
                {
                    _placeholdernumber = value;
                    PropertyHasChanged("placeholdernumber");
                }
            }
        }

        public string fieldname
        {
            get
            {
                CanReadProperty("fieldname", true);
                return _fieldname;
            }
            set
            {
                CanWriteProperty("fieldname", true);
                if (value == null)
                    value = string.Empty;
                if (!_fieldname.Equals(value))
                {
                    _fieldname = value;
                    PropertyHasChanged("fieldname");
                }
            }
        }

        public int fieldtype
        {
            get
            {
                CanReadProperty("fieldtype", true);
                return _fieldtype;
            }
            set
            {
                CanWriteProperty("fieldtype", true);
                if (!_fieldtype.Equals(value))
                {
                    _fieldtype = value;
                    PropertyHasChanged("fieldtype");
                }
            }
        }

        public int fieldheight
        {
            get
            {
                CanReadProperty("fieldheight", true);
                return _fieldheight;
            }
            set
            {
                CanWriteProperty("fieldheight", true);
                if (!_fieldheight.Equals(value))
                {
                    _fieldheight = value;
                    PropertyHasChanged("fieldheight");
                }
            }
        }

        public int fieldwidth
        {
            get
            {
                CanReadProperty("fieldwidth", true);
                return _fieldwidth;
            }
            set
            {
                CanWriteProperty("fieldwidth", true);
                if (!_fieldwidth.Equals(value))
                {
                    _fieldwidth = value;
                    PropertyHasChanged("fieldwidth");
                }
            }
        }

        public int editortype
        {
            get
            {
                CanReadProperty("editortype", true);
                return _editortype;
            }
            set
            {
                CanWriteProperty("editortype", true);
                if (!_editortype.Equals(value))
                {
                    _editortype = value;
                    PropertyHasChanged("editortype");
                }
            }
        }

        public Boolean ShowName
        {
            get
            {
                return _ShowName;
            }
            set
            {
                CanWriteProperty("ShowName", true);
                if (!_ShowName.Equals(value))
                {
                    _ShowName = value;
                    PropertyHasChanged("ShowName");
                }
            }
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        #endregion //Business Properties and Methods

        #region Validation Rules
        private void AddCustomRules()
        {
            //add custom/non-generated rules here...
        }

        private void AddCommonRules()
        {
            //
            // fieldname
            //
            ValidationRules.AddRule(CommonRules.StringRequired, "fieldname");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new CommonRules.MaxLengthRuleArgs("fieldname", 50));
        }

        protected override void AddBusinessRules()
        {
            AddCommonRules();
            AddCustomRules();
        }
        #endregion //Validation Rules

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in TemplateChildDetail
            //AuthorizationRules.AllowRead("id", "TemplateChildDetailReadGroup");
            //AuthorizationRules.AllowRead("templateid", "TemplateChildDetailReadGroup");
            //AuthorizationRules.AllowRead("placeholdernumber", "TemplateChildDetailReadGroup");
            //AuthorizationRules.AllowRead("fieldname", "TemplateChildDetailReadGroup");
            //AuthorizationRules.AllowRead("fieldtype", "TemplateChildDetailReadGroup");
            //AuthorizationRules.AllowRead("fieldheight", "TemplateChildDetailReadGroup");
            //AuthorizationRules.AllowRead("fieldwidth", "TemplateChildDetailReadGroup");
            //AuthorizationRules.AllowRead("editortype", "TemplateChildDetailReadGroup");

            //AuthorizationRules.AllowWrite("placeholdernumber", "TemplateChildDetailWriteGroup");
            //AuthorizationRules.AllowWrite("fieldname", "TemplateChildDetailWriteGroup");
            //AuthorizationRules.AllowWrite("fieldtype", "TemplateChildDetailWriteGroup");
            //AuthorizationRules.AllowWrite("fieldheight", "TemplateChildDetailWriteGroup");
            //AuthorizationRules.AllowWrite("fieldwidth", "TemplateChildDetailWriteGroup");
            //AuthorizationRules.AllowWrite("editortype", "TemplateChildDetailWriteGroup");
        }


        public static bool CanGetObject()
        {
            //TODO: Define CanGetObject permission in TemplateChildDetail
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("TemplateChildDetailViewGroup"))
            //	return true;
            //return false;
        }

        public static bool CanAddObject()
        {
            //TODO: Define CanAddObject permission in TemplateChildDetail
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("TemplateChildDetailAddGroup"))
            //	return true;
            //return false;
        }

        public static bool CanEditObject()
        {
            //TODO: Define CanEditObject permission in TemplateChildDetail
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("TemplateChildDetailEditGroup"))
            //	return true;
            //return false;
        }

        public static bool CanDeleteObject()
        {
            //TODO: Define CanDeleteObject permission in TemplateChildDetail
            return true;
            //if (Csla.ApplicationContext.User.IsInRole("TemplateChildDetailDeleteGroup"))
            //	return true;
            //return false;
        }
        #endregion //Authorization Rules

        #region Factory Methods
        private TemplateChildDetail()
        { /* require use of factory method */
        }

        public static TemplateChildDetail NewTemplateChildDetail()
        {
            if (!CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a TemplateChildDetail");
            return DataPortal.Create<TemplateChildDetail>();
        }

        public static TemplateChildDetail GetTemplateChildDetail(Guid templateid)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a TemplateChildDetail");
            return DataPortal.Fetch<TemplateChildDetail>(new Criteria(templateid));
        }

        public static TemplateChildDetail GetTemplateChildDetail(Guid templateid, int placeholdernumber)
        {
            if (!CanGetObject())
                throw new System.Security.SecurityException("User not authorized to view a TemplateChildDetail");
            return DataPortal.Fetch<TemplateChildDetail>(new Criteria(templateid, placeholdernumber));
        }

        public static void DeleteTemplateChildDetail(Guid id)
        {
            if (!CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a TemplateChildDetail");
            DataPortal.Delete(new Criteria(id));
        }

        public override TemplateChildDetail Save()
        {
            if (IsDeleted && !CanDeleteObject())
                throw new System.Security.SecurityException("User not authorized to remove a TemplateChildDetail");
            else if (IsNew && !CanAddObject())
                throw new System.Security.SecurityException("User not authorized to add a TemplateChildDetail");
            else if (!CanEditObject())
                throw new System.Security.SecurityException("User not authorized to update a TemplateChildDetail");

            return base.Save();
        }

        #endregion //Factory Methods

        #region Data Access

        #region Criteria

        [Serializable()]
        private class Criteria
        {
            public Guid templateid;
            public int placeholdernumber;

            public Criteria(Guid templateid)
            {
                this.templateid = templateid;
            }

            public Criteria(Guid templateid, int placeholdernumber)
            {
                this.templateid = templateid;
                this.placeholdernumber = placeholdernumber;
            }
        }

        #endregion //Criteria

        #region Data Access - Create
        [RunLocal]
        protected override void DataPortal_Create()
        {
            ValidationRules.CheckRules();
        }

        #endregion //Data Access - Create

        #region Data Access - Fetch
        private void DataPortal_Fetch(Criteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            DAL.AddParameter("@templateid", criteria.templateid);
            DAL.AddParameter("@placeholdernumber", criteria.placeholdernumber);

            using(SafeDataReader dr = new SafeDataReader(DAL.ExecuteReader(Procs.GetTemplateByTemplateID.ToString().Trim(), CommandType.Text, ConnectionState.Open)))
            {
                FetchObject(dr);
                ValidationRules.CheckRules();

                //load child object(s)
                FetchChildren(dr);
            }
        }

        private void FetchObject(SafeDataReader dr)
        {
            dr.Read();
            //_id = dr.GetGuid("id");
            //_templateid = dr.GetGuid("templateid");
            _placeholdernumber = dr.GetInt32("placeholdernumber");
            _fieldname = dr.GetString("fieldname");
            _fieldtype = dr.GetInt32("fieldtype");
            _fieldheight = dr.GetInt32("fieldheight");
            _fieldwidth = dr.GetInt32("fieldwidth");
            _editortype = dr.GetInt32("editortype");
            _ShowName = dr.GetBoolean("showname");
        }

        private void FetchChildren(SafeDataReader dr)
        {
        }
        #endregion //Data Access - Fetch

        #region Data Access - Insert
        protected override void DataPortal_Insert()
        {
            ExecuteInsert();
        }

        private void ExecuteInsert()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddInsertParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.InsertTemplate.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddInsertParameters(ref BA.DAL.DBUtility DAL)
        {
            DAL.AddParameter("@id", _id);
            DAL.AddParameter("@templateid", _templateid);
            DAL.AddParameter("@placeholdernumber", _placeholdernumber);
            DAL.AddParameter("@fieldname", _fieldname);
            
            if (_fieldtype != 0)
                DAL.AddParameter("@fieldtype", _fieldtype);
            else
                DAL.AddParameter("@fieldtype", DBNull.Value);
            
            DAL.AddParameter("@fieldheight", _fieldheight);
            DAL.AddParameter("@fieldwidth", _fieldwidth);
            
            if (_editortype != 0)
                DAL.AddParameter("@editortype", _editortype);
            else
                DAL.AddParameter("@editortype", DBNull.Value);

            DAL.AddParameter("@showname", _ShowName);
        }
        #endregion //Data Access - Insert

        #region Data Access - Update
        protected override void DataPortal_Update()
        {
            if (base.IsDirty)
            {
                ExecuteUpdate();
            }
        }

        private void ExecuteUpdate()
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddUpdateParameters(ref DAL);

            DAL.ExecuteNonQuery(Procs.UpdateTemplate.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddUpdateParameters(ref BA.DAL.DBUtility DAL)
        {
            DAL.AddParameter("@templateid", _templateid);
            DAL.AddParameter("@placeholdernumber", _placeholdernumber);
            DAL.AddParameter("@fieldname", _fieldname);
            if (_fieldtype != 0)
                DAL.AddParameter("@fieldtype", _fieldtype);
            else
                DAL.AddParameter("@fieldtype", DBNull.Value);
            DAL.AddParameter("@fieldheight", _fieldheight);
            DAL.AddParameter("@fieldwidth", _fieldwidth);
            if (_editortype != 0)
                DAL.AddParameter("@editortype", _editortype);
            else
                DAL.AddParameter("@editortype", DBNull.Value);

            DAL.AddParameter("@showname", _ShowName);
        }

        private void UpdateChildren(SqlConnection cn)
        {
        }
        #endregion //Data Access - Update

        #region Data Access - Delete
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new Criteria(_id));
        }

        private void DataPortal_Delete(Criteria criteria)
        {
            ExecuteDelete(criteria);
        }

        private void ExecuteDelete(Criteria criteria)
        {
            DAL.CreateDBObjects(Config.ConnectionString, Config.Provider);

            AddDeleteParameters(ref DAL, criteria);

            DAL.ExecuteNonQuery(Procs.DeleteTemplate.ToString().Trim(), CommandType.Text, ConnectionState.Open);
        }

        private void AddDeleteParameters(ref BA.DAL.DBUtility DAL, Criteria criteria)
        {
            DAL.AddParameter("@templateid", criteria.templateid);
            DAL.AddParameter("@placeholdernumber", criteria.placeholdernumber);
        }
        #endregion //Data Access - Delete

        #endregion //Data Access
    }
}
